module CalVar where					
import ExprDataTypes		

funppp :: Combo -> Integer -> (VarMap, Integer)
funppp (Mas a b) cont = ((v1) ++ (v2), c2)
                        where (v1,c1) = funppp (fst a) cont
                              (v2,c2) = funppp (fst b) c1
funppp (Menos a b) cont = ((v1) ++ (v2), c2)
                          where (v1,c1) = funppp (fst a) cont
                                (v2,c2) = funppp (fst b) c1
funppp (Por a b) cont = ((v1) ++ (v2), c2)
                         where (v1,c1) = funppp (fst a) cont
                               (v2,c2) = funppp (fst b) c1
funppp (Div a b) cont = ((v1) ++ (v2), c2)
                        where (v1,c1) = funppp (fst a) cont
                              (v2,c2) = funppp (fst b) c1 
funppp (Igual a b) cont = ((v1) ++ (v2), c2)
                          where (v1,c1) = funppp (fst a) cont
                                (v2,c2) = funppp (fst b) c1
funppp (Menor a b) cont = ((v1) ++ (v2), c2)
                          where (v1,c1) = funppp (fst a) cont
                                (v2,c2) = funppp (fst b) c1
funppp (Mayor a b) cont = ((v1) ++ (v2), c2)
                          where (v1,c1) = funppp (fst a) cont
                                (v2,c2) = funppp (fst b) c1
funppp (Parentesis a) cont = funppp (fst a) cont
funppp (ONot a) cont = funppp (fst a) cont
funppp (OAnd a b) cont = ((v1) ++ (v2), c2)
                         where (v1,c1) = funppp (fst a) cont
                               (v2,c2) = funppp (fst b) c1
funppp (OOr a b) cont = ((v1) ++ (v2), c2)
                        where (v1,c1) = funppp (fst a) cont
                              (v2,c2) = funppp (fst b) c1                            
funppp (Distinto a b) cont = ((v1) ++ (v2), c2)
                             where (v1,c1) = funppp (fst a) cont
                                   (v2,c2) = funppp (fst b) c1
funppp (Asignacion a b) cont = funppp (fst b) cont
funppp (Incremento a) cont = funppp (fst a) cont  
funppp (Decremento a) cont = funppp (fst a) cont
funppp (MayorIgual a b) cont = ((v1) ++ (v2), c2)
                               where (v1,c1) = funppp (fst a) cont
                                     (v2,c2) = funppp (fst b) c1
funppp (MenorIgual a b) cont = ((v1) ++ (v2), c2)
                         where (v1,c1) = funppp (fst a) cont
                               (v2,c2) = funppp (fst b) c1
                               
funppp (Declaracion tipo var (EOpc a)) cont = (v1 ++ [(tipo,var,(c1+1))],c1+1)
                                              where (v1,c1) = funppp (fst a) cont


funppp (Declaracion tipo var NoExp) cont = ([(tipo,var,(cont+1))],cont+1)

funppp (Condicional cond b1 (BElse b2)) cont = ((v1) ++ (v2) ++ (v3) ++ [("bool","if"++show (c3+1),(c3+1))],c3+1)
                                             where (v1,c1) = funppp (fst cond) cont
                                                   (v2,c2) = calvariables (map fst b1) c1
                                                   (v3,c3) = calvariables (map fst b2) c2											
funppp (Condicional cond b NoBloque) cont = ((v1) ++ (v2) ++ [("bool","if"++show (c2+1),(c2+1))],c2+1)
                                            where (v1,c1) = funppp (fst cond) cont
                                                  (v2,c2) = calvariables (map fst b) c1 		

funppp (Iteracion cond b1 (BElse b2)) cont = ((v1) ++ (v2) ++ (v3) ++ [("bool","while"++show (c3+1),(c3+1))],c3+1)
                                           where (v1,c1) = funppp (fst cond) cont
                                                 (v2,c2) = calvariables (map fst b1) c1
                                                 (v3,c3) = calvariables (map fst b2) c2
funppp (Iteracion cond b NoBloque) cont = ((v1) ++ (v2) ++ [("bool","while"++ show (c2+1),(c2+1))],c2+1)
                                          where (v1,c1) = funppp (fst cond) cont
                                                (v2,c2) = calvariables (map fst b) c1

funppp c cont = ([],cont) 		


calvariables :: [Combo] -> Integer -> (VarMap,Integer)

calvariables [] n = ([],n)

calvariables (x:xs) n =  (v ++ v1,nf2)
                         where (v,nf) = funppp x (n)
                               (v1,nf2) = calvariables xs nf

